package com.jit.monitor.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.jit.baselib.ui.base.BaseFragment;
import com.jit.baselib.ui.base.BasePresenter;
import com.jit.monitor.R;

import butterknife.BindView;

/**
 * @author zxl on 2018/8/17.
 *         discription:
 */

public class DetFragment extends LazyFragment implements View.OnKeyListener {
    private TextView mTv;

    @Override
    public void initViews(View view,Bundle savedInstanceState) {
        mTv = view.findViewById(R.id.tv);
        Log.e("initView"," ----初始化 第一个Fragment-----");
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
        mTv.setText(DetFragment.class.getName());
        Log.e("initData"," ---- 懒加载 第一个Fragment-----");
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_launch;
    }

    @Override
    public void onResume() {
        super.onResume();
        final View rootView = getView();
        if (rootView != null){
            rootView.setFocusableInTouchMode(true);
            rootView.requestFocus();
            rootView.setOnKeyListener(this);
        }
    }

    private long mExitTime = 0;
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if ((System.currentTimeMillis() - mExitTime) > 2000){
                Toast.makeText(getContext(), "双击退出", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            }else {
                getActivity().finish();
                if (mExitTime != 0){
                    mExitTime = 0;
                }
                return true;
            }
        }
        return false;
    }
}
